#version 450 core
out vec4 fragment_color;
in vec3 volume_position;
layout(location = 3) uniform sampler3D	volume;
layout(location = 4) uniform vec3		camera_position;
layout(location = 5) uniform vec3		step_size;
layout(location = 6) uniform float 		threshold  = 0.2;
const int MAX_SAMPLES = 300;
const vec3 texture_min = vec3(0);
const vec3 texture_max = vec3(1);
void main()
{ 
	vec3 data_position = volume_position;
	vec3 ray_direction = normalize((volume_position-vec3(0.5)) - camera_position); 
	vec3 ray_step = ray_direction * step_size; 
	bool stop = false;
	fragment_color = vec4(0.0);
	for (int i = 0; i < MAX_SAMPLES; i++){
		data_position = data_position + ray_step;
		stop = dot(sign(data_position - texture_min), sign(texture_max - data_position)) < 3.0;
		if (stop) 
			break;
		float intensity = texture(volume, data_position).r;	
		float previous_alpha = intensity - (intensity * fragment_color.a);
		fragment_color.rgb = previous_alpha * vec3(intensity) + fragment_color.rgb; 
		fragment_color.a += previous_alpha * threshold; 
		if( fragment_color.a>0.99)
			break;
	} 
}